package org.example.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class FileGenerator {
    public static void main(String[] args) {
        String url = "jdbc:clickhouse://host:port/default";
        String user = "username";
        String password = "password";

        int rowLimit = 10000; // 每个文件的行数限制
        long fileLimit = 235000000; // 文件大小限制，单位为字节（235MB）
        long currentFileSize = 0;
        int fileCount = 1;

        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM table A");

            BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/data/clickhouse/output_" + fileCount + ".txt")));

            while (resultSet.next()) {
                String row = resultSet.getString("column1") + "\t" + resultSet.getString("column2") + "\t..."; // 生成行数据

                // 估计写入行的字节数
                int rowBytes = row.getBytes().length;

                if (currentFileSize + rowBytes >= fileLimit) {
                    writer.close();
                    fileCount++;
                    currentFileSize = 0;
                    writer = new BufferedWriter(new FileWriter(new File("/data/clickhouse/output_" + fileCount + ".txt")));
                }

                writer.write(row + System.lineSeparator());  // 写入数据到文件
                currentFileSize += rowBytes;  // 更新当前文件大小
            }
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
